#include "liveviewer.h"
#include <QtDebug>
#include <QChar>
#include <QList>
#include <QStringList>
#include "graphicsiplimgobject.h"
#include <QLabel>
#include "trace.h"

LiveViewer::LiveViewer(QWidget *parent)
    : QWidget(parent)
{
    initWindow();
}

LiveViewer::~LiveViewer()
{

}

void LiveViewer::initWindow()
{
    /*Toolbox*/
    buttonRefreshNodeList = new QPushButton(tr("Refresh Nodes"));
    buttonRefreshNodeList->setMaximumWidth(120);
    buttonConnectNode = new QPushButton(tr("Connect!"));
    buttonConnectNode->setMaximumWidth(80);
    buttonLiveView = new QPushButton(tr("View Node"));
    buttonLiveView->setMaximumWidth(120);

    /*Node List*/
    treeNodeList = new QTreeWidget();
    treeNodeList->setHeaderLabel(tr("Nodes"));
    treeNodeList->setFixedWidth(150);
    cpPFSettings = new CP_PFCapture();
    initPFNodes();
    if(pfNodes.size()>0)
    {
        for(int i=0;i<pfNodes.size();i++)
        {
            PFNode *pf = pfNodes.at(i);
            std::string stdName = pf->getNodeName();
            QString nodeName(stdName.c_str());
            QTreeWidgetItem *node = new QTreeWidgetItem(treeNodeList, QStringList(nodeName));
        }
    }

    initScene();

    /*Zoom Slider*/
    sliderZoom = new QSlider(Qt::Vertical);
    sliderZoom->setMinimum(0);
    sliderZoom->setMaximum(500);
    sliderZoom->setValue(250);
    sliderZoom->setTickPosition(QSlider::TicksRight);

    initThumbScene();

    /*Layout Begin*/

    viewGLayout = new QGridLayout;
    viewGLayout->setSpacing(2);
    viewGLayout->setMargin(2);
    viewGLayout->addWidget(treeNodeList,0,0);
    viewGLayout->addWidget(cpPFSettings,1,0);
    viewGLayout->setColumnMinimumWidth(0,100);
    viewGLayout->addWidget(camView,0,1);
    viewGLayout->setColumnMinimumWidth(1,800);
    viewGLayout->addWidget(sliderZoom,0,2);
    viewGLayout->addWidget(thumbView,1,1);

    toolboxHLayout=new QHBoxLayout;
    toolboxHLayout->setAlignment(Qt::AlignLeft);
    toolboxHLayout->addWidget(buttonRefreshNodeList);
    toolboxHLayout->addWidget(buttonConnectNode);
    toolboxHLayout->addWidget(buttonLiveView);

    mainVLayout=new QVBoxLayout;
    mainVLayout->addLayout(toolboxHLayout);
    mainVLayout->addLayout(viewGLayout);

    this->setLayout(mainVLayout);
    //this->resize(720,600);
    /*Layout End*/

    //client = new LiveClient();

    connect(buttonConnectNode,SIGNAL(clicked()),SLOT(onConnectNodeClicked()));
    connect(buttonLiveView,SIGNAL(clicked()),SLOT(onViewNodeClicked()));

}

void LiveViewer::initScene()
{
    camScene = new QGraphicsScene;
    camView = new QGraphicsView;
    camView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    camView->setDragMode(QGraphicsView::RubberBandDrag);
    camView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    camView->setScene(camScene);
    camView->setMinimumWidth(500);

    PFNode *pf = pfNodes.at(0);
    GraphicsIplImgObject *item = pf->getLiveImage();
    item->setPos(0,0);
    camScene->addItem(item);
    pf->setToWatching(0);
}

void LiveViewer::initThumbScene()
{
    thumbScene = new QGraphicsScene;
    thumbView = new QGraphicsView;
    thumbView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    thumbView->setDragMode(QGraphicsView::ScrollHandDrag);
    thumbView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    thumbView->setScene(thumbScene);
    thumbView->setFixedHeight(200);
    thumbView->setMinimumWidth(500);

    for(int i=0;i<pfNodes.size();i++)
    {
        PFNode *pf = pfNodes.at(i);
        QList<GraphicsIplImgObject *> items = pf->getThumbImages();
        for(int j=0;j<PFCMU::TOTALCAMS;j++)
        {
            int xpos = j*(PFCMU::WIDTH/4+20);
            items.at(j)->setPos(xpos,0);
            if(i==0)
            {
                thumbScene->addItem(items.at(j));
            }
            //connect(items.at(j),SIGNAL(objectDoubleClicked(int)),pf,SLOT(setToWatching(int)));
        }
    }
}

void LiveViewer::initPFNodes()
{
    for(int i=0;i<20;i++)
    {
        QString nodeName = QString("%1%2").arg(DOMENODE::NODEIP).arg(i+1,2,10,QChar('0'));
        PFNode *node = new PFNode(nodeName.toStdString());
        pfNodes.push_back(node);
        connect(this,SIGNAL(connectButtonClicked()),node,SLOT(startConnection()));
        connect(cpPFSettings,SIGNAL(startCapture(QStringList)),node,SLOT(startCapture(QStringList)));
        connect(node,SIGNAL(allImageDataReceived()),SLOT(onAllImageDataReceived()));
    }
}


void LiveViewer::onConnectNodeClicked()
{
    emit connectButtonClicked();
}

void LiveViewer::onViewNodeClicked()
{
    //TODO
    QList<QTreeWidgetItem *> selectedItems = treeNodeList->selectedItems();
    QTreeWidgetItem *item;
    QString name;
    int idx;
    if(selectedItems.count() != 0)
    {
        item = selectedItems.at(0);
        name = QString(item->text(0));
        QString strIdx = name.right(2);
        idx = strIdx.toInt()-1;
    }
    for(int i=0;i<pfNodes.size();i++)
    {
        PFNode *pf = pfNodes.at(i);
        pf->setNotWatching();
    }
    //idx=0;
    PFNode *pf = pfNodes.at(idx);
    pf->setToWatching(0);

    QList<QGraphicsItem *> camobj = camScene->items();
    for(int i=0;i<camobj.count();i++)
    {
        camScene->removeItem(camobj.at(i));
    }
    QList<QGraphicsItem *> thumobj = thumbScene->items();
    for(int i=0;i<thumobj.count();i++)
    {
        thumbScene->removeItem(thumobj.at(i));
    }
    QList<GraphicsIplImgObject *> thums = pf->getThumbImages();
    for(int i=0;i<thums.count();i++)
    {
        thumbScene->addItem(thums.at(i));
    }
    camScene->addItem(pf->getLiveImage());
    thumbView->update();
    camView->update();
}

void LiveViewer::onAllImageDataReceived()
{
    //TRACE(1,"GUI Update\n");
    thumbScene->update();
    //thumbView->repaint();
    camScene->update();
    //camView->repaint();
}
